# 打印一个字符串的全部排列 打印一个字符串的全部排列，要求不要出现重复的排列
# 输入："abc"
# 输出：['abc', 'acb', 'bac', 'bca', 'cba', 'cab']


def all_permutations(s: str):
    res = []
    s = list(s)
    process(s, 0, res)
    return res


def process(s: list, k: int, res: list):
    if k == len(s):
        res.append(''.join(s))
    visited = set()
    for i in range(k, len(s)):
        if s[i] not in visited:
            visited.add(s[i])
            s[k], s[i] = s[i], s[k]
            process(s, k+1, res)
            s[k], s[i] = s[i], s[k]


if __name__ == "__main__":
    print(all_permutations("aac"))
